﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiPCCreateComponentPreviewCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using COMMANDPARAMSLib;
using DnProcessSimulateCommands.PreviewComponents;
using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class CUiPCCreateComponentPreviewCmd : TxButtonCommand
{
  public virtual string Category => ComponentPreviewResource.CREATE_COMP_PREVIEW_CMD_CATEGORY;

  public virtual string Name => ComponentPreviewResource.CREATE_COMP_PREVIEW_CMD_NAME;

  public virtual string Tooltip => ComponentPreviewResource.CREATE_COMP_PREVIEW_CMD_TOOTIP;

  public virtual string Description => ComponentPreviewResource.CREATE_COMP_PREVIEW_CMD_DESC;

  public virtual void Execute(object cmdParams)
  {
    if (cmdParams == null)
      this.ExecuteWithUI();
    else
      this.ExecuteWithParametersWithoutUI(cmdParams);
  }

  private void ExecuteWithUI()
  {
    using (TxFolderBrowserDialog folderBrowserDialog = new TxFolderBrowserDialog())
    {
      folderBrowserDialog.InitialFolder = TxApplication.SystemRootDirectory;
      if (folderBrowserDialog.ShowDialog(TxApplicationEx.GetMainWindowHandle(IntPtr.Zero)) != 1)
        return;
      string str = folderBrowserDialog.SelectedFolder + "\\";
      string systemRootDirectory = TxApplication.SystemRootDirectory;
      if (!str.StartsWith(systemRootDirectory, StringComparison.OrdinalIgnoreCase) && !str.Equals(systemRootDirectory, StringComparison.OrdinalIgnoreCase))
      {
        int num = (int) TxMessageBox.Show(ComponentPreviewResource.PATH_ISNT_UNDER_SYSTEM_ROOT, ComponentPreviewResource.CREATE_COMP_PREVIEW_CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
      else
      {
        if (!new CApPCPreviewComponentApp().CreateComponentsPreviewImage(folderBrowserDialog.SelectedFolder))
          return;
        TxApplication.ActiveUndoManager.ClearAllTransactions();
      }
    }
  }

  private void ExecuteWithParametersWithoutUI(object cmdParams)
  {
    if (!(cmdParams is ICoMultipleObjectParams multipleObjectParams))
      return;
    CApPCPreviewComponentApp previewComponentApp = new CApPCPreviewComponentApp();
    ITApiObjCollection ppObjs;
    multipleObjectParams.GetObjList(ref ppObjs);
    foreach (ITxObject txObject in (Collection<ITxObject>) this.ConvertApiObjCollToTxObjectList(ppObjs))
    {
      if (txObject is ITxComponent component)
        previewComponentApp.CreateComponentPreviewImage(component);
    }
  }

  private TxObjectList ConvertApiObjCollToTxObjectList(ITApiObjCollection ppObjs)
  {
    TxObjectList txObjectList = new TxObjectList();
    if (ppObjs != null)
    {
      foreach (ITApiObj ppObj in ppObjs)
      {
        ITxObject objectFromApiObj = CUiPCCreateComponentPreviewCmd.GetObjectFromApiObj(ppObj);
        if (objectFromApiObj != null)
          ((Collection<ITxObject>) txObjectList).Add(objectFromApiObj);
      }
    }
    return txObjectList;
  }

  private static ITxObject GetObjectFromApiObj(ITApiObj apiObj)
  {
    string str = (string) null;
    apiObj.DumpToString(ref str);
    ITxObject objectFromApiObj = (ITxObject) null;
    if (str != null)
      objectFromApiObj = TxConvertor.tID2Object(str);
    return objectFromApiObj;
  }
}
